import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // 模拟能力模板数据
  const mockTemplates = [
    {
      id: 'workflow-booking',
      name: '预订工作流模板',
      description: '标准房间预订流程模板',
      type: 'workflow',
      category: 'booking',
      version: '1.0.0',
      config: {
        steps: [
          { id: 'check-availability', name: '检查可用性', type: 'condition' },
          { id: 'create-booking', name: '创建预订', type: 'action' },
          { id: 'send-confirmation', name: '发送确认', type: 'notification' },
        ],
      },
      dependencies: ['room-resource', 'notification-service'],
      tags: ['工作流', '预订', '酒店'],
      icon: 'booking-icon',
    },
    {
      id: 'form-room-config',
      name: '房间配置表单模板',
      description: '房间属性配置表单模板',
      type: 'form',
      category: 'room',
      version: '1.0.0',
      config: {
        fields: [
          {
            name: 'roomNumber',
            label: '房间号',
            type: 'input',
            required: true,
          },
          {
            name: 'roomType',
            label: '房间类型',
            type: 'select',
            options: ['标准间', '豪华间', '套房'],
          },
          { name: 'price', label: '价格', type: 'number', required: true },
          {
            name: 'amenities',
            label: '设施',
            type: 'checkbox',
            options: ['WiFi', '空调', '电视', '冰箱'],
          },
        ],
      },
      dependencies: [],
      tags: ['表单', '房间', '配置'],
      icon: 'form-icon',
    },
    {
      id: 'rule-pricing',
      name: '定价规则模板',
      description: '动态定价规则模板',
      type: 'rule',
      category: 'pricing',
      version: '1.0.0',
      config: {
        conditions: [
          {
            field: 'occupancy',
            operator: 'gt',
            value: 0.8,
            action: 'increase',
            value: 0.1,
          },
          {
            field: 'season',
            operator: 'eq',
            value: 'peak',
            action: 'multiply',
            value: 1.2,
          },
        ],
      },
      dependencies: ['pricing-service'],
      tags: ['规则', '定价', '动态'],
      icon: 'rule-icon',
    },
    {
      id: 'report-occupancy',
      name: '入住率报表模板',
      description: '房间入住率统计报表模板',
      type: 'report',
      category: 'analytics',
      version: '1.0.0',
      config: {
        metrics: ['occupancy_rate', 'revenue_per_room', 'average_stay'],
        dimensions: ['date', 'room_type', 'floor'],
        chartType: 'line',
      },
      dependencies: ['analytics-service'],
      tags: ['报表', '入住率', '统计'],
      icon: 'report-icon',
    },
    {
      id: 'api-payment',
      name: '支付API模板',
      description: '第三方支付接口模板',
      type: 'api',
      category: 'payment',
      version: '1.0.0',
      config: {
        endpoints: [
          {
            path: '/payment/create',
            method: 'POST',
            description: '创建支付订单',
          },
          {
            path: '/payment/query',
            method: 'GET',
            description: '查询支付状态',
          },
          { path: '/payment/refund', method: 'POST', description: '退款处理' },
        ],
        authentication: 'oauth2',
      },
      dependencies: ['payment-gateway'],
      tags: ['API', '支付', '接口'],
      icon: 'api-icon',
    },
  ];

  // 根据查询条件过滤
  let filteredTemplates = mockTemplates;

  if (query.type) {
    filteredTemplates = filteredTemplates.filter((t) => t.type === query.type);
  }

  if (query.category) {
    filteredTemplates = filteredTemplates.filter(
      (t) => t.category === query.category,
    );
  }

  if (query.tags) {
    const tags = Array.isArray(query.tags) ? query.tags : [query.tags];
    filteredTemplates = filteredTemplates.filter((t) =>
      tags.some((tag) => t.tags.includes(tag as string)),
    );
  }

  return filteredTemplates;
});
